import java.util.*;


public class Node {
	
	ArrayList<Node> children = new ArrayList<Node>();
	ArrayList<String> attributes = new ArrayList<String>();
	
	Node parent;
	String phrase;
	
	public Node()				{	init();						}
	public Node(Node p)			{	parent = p;		init();		}
	public void init()			{	phrase = "";				}
	public void add(Node a)		{	children.add(a);			}
	public void add(String s)	{	attributes.add(s);			}
	public int nbChildren()		{	return children.size();		}
	public Node getChild(int i)	{	return children.get(i);		}
	public String getType()		{	return(attributes.get(0));	}
	
	public String getAttribute(String key){	return getAttributes().get(key);	}
	
	
	public Hashtable<String, String> getAttributes(){
		Hashtable<String, String> result = new Hashtable<String, String>();
		for(int i = 1; i < attributes.size(); i = i+2)
			result.put(attributes.get(i), attributes.get(i+1));
		return result;
	}
	
	public ArrayList<Node> getAllofType(String type){
		ArrayList<Node> result = new ArrayList<Node>();
		if(parent != null && getType().equalsIgnoreCase(type)) result.add(this);
		for(Node child: children){
			ArrayList<Node> temp = child.getAllofType(type);
			if(temp != null && !temp.isEmpty()) result.addAll(temp);
		}
		return result;
	}
	
	public String toString(){
		String result = "<";
		for(String s: attributes){
			result = result + s + " ";
		}
		result = result + "> " + phrase + " </" + attributes.get(0) +">\n";
		return result;
	}
	
	public void print(){
		if(attributes.size() != 0){
			System.out.println(getType() + ": " + phrase);	
		}
		for(Node child: children){
			child.print();
		}
	}
	
}
